package com.dmai.aiet.archive.service.impl;

import com.dmai.aiet.archive.entity.SysUser;
import com.dmai.aiet.archive.repository.SysUserRepository;
import com.dmai.aiet.archive.service.ISysUserService;
import com.dmai.aiet.common.base.PageResult;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.r2dbc.core.R2dbcEntityTemplate;
import org.springframework.data.relational.core.query.Criteria;
import org.springframework.data.relational.core.query.Query;
import org.springframework.data.util.Streamable;
import org.springframework.r2dbc.core.DatabaseClient;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.Map;

@Slf4j
@Service
public class SysUserServiceImpl implements ISysUserService {
    @Resource
    private SysUserRepository userRepository;

    @Resource
    private DatabaseClient client;

    @Resource
    private R2dbcEntityTemplate template;

    @Override
    public Flux<SysUser> getAllUserInfo() {
        return userRepository.findAll();
    }

    @Transactional(readOnly = true)
    public Mono<PageResult<SysUser>> getPageUserInfo(Map<String, Object> params) {
        String username = params.containsKey("username") ? params.get("username").toString() : null;
        String mobile = params.containsKey("mobile") ? params.get("mobile").toString() : null;

        Integer pageSize = params.containsKey("pageSize") ? Integer.valueOf(params.get("pageSize").toString()) : 20;
        Integer pageNumber = params.containsKey("pageNumber") ? Integer.valueOf(params.get("pageNumber").toString()) : 0;

        // 查询条件
        var criteria = Criteria.empty();
        if (StringUtils.isNotEmpty(username)) {
            // 如果name不为null，则模糊查询name
            criteria = criteria.and("username").like("%" + username + "%");
        }
        if (StringUtils.isNotEmpty(mobile)) {
            // 如果size不为null，则 size > $size
            criteria = criteria.and("mobile").greaterThan(mobile);
        }
        // 构建Query
        var page = PageRequest.of(pageNumber, pageSize);
        var query = Query.query(criteria).with(page);

        return Mono.zip(
                template.count(query, SysUser.class),
                template.select(query, SysUser.class).collectList(),
                PageResult::new
        );
    }

    @Override
    public Mono<SysUser> getUserById(Long id) {
        return userRepository.findById(id);
    }

    @Override
    public Mono<SysUser> saveUser(SysUser user) {
        return userRepository.save(user);
    }

    @Override
    public Mono<SysUser> updateUser(Long id, SysUser user) {
        user.setId(id);
        return userRepository.updateUser(user);
    }

    @Override
    public Mono<Void> deleteUser(Long id) {
        return userRepository.deleteById(id);
    }
}
